#include <stdlib.h>
#include <omp.h>
#include <stdio.h>
#include <time.h>
//#define NUM_THREADS 16
//编译指令
//gcc -std=c99 -fopenmp 15SpeedTest1.c -o cmake-build/15SpeedTest1 && export OMP_NUM_THREADS=512 && time ./cmake-build/15SpeedTest1

void test(FILE*fp,int thread)
{
    omp_set_num_threads(thread);
    static long num_steps=1e10;
    double      step     =1.0f/(double)num_steps;
    
    double sum=0.0;
    
    clock_t start=clock();
#pragma omp parallel for reduction(+:sum) shared(num_steps,step) default(none)
    for(long long i=0;i<num_steps;i++)
    {
        //printf("%d",omp_get_num_threads());
        long double x=(i+0.5)*step;
        sum+=4.0/(1.0+x*x);
    }
    
    double pi=step*sum;
    double time=(double)(clock()-start)/CLOCKS_PER_SEC;
    printf("%d thread Pi=%21.20lf(%ld steps) %lfs\n",omp_get_max_threads(),pi,num_steps,time);
    fflush(stdout);
    fprintf(fp,"%d,%lf\n",omp_get_max_threads(),time);
}

int main()
{
    FILE*fp=fopen("15SpeedTest1.csv","wb");
    
    for(int i=1;i<=128;++i)
        test(fp,i);
    
    fclose(fp);
    
    return 0;
}
